package com.suotuo.module.sys.service.impl;

import com.suotuo.module.sys.dao.CategoryDAO;
import com.suotuo.module.sys.entity.Category;
import com.suotuo.module.sys.entity.Games;
import com.suotuo.module.sys.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liulubingjava@163.com
 * @data 2019/1/18 16:52
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryDAO categoryDAO;
    @Override
    public Category getCategory(String categoryId) {
        return categoryDAO.findById(categoryId).get();
    }

    @Override
    public Page<Category> findAll() {
        PageRequest pageRequest = PageRequest.of(0,15);
        Page<Category> categoryPage  = categoryDAO.findAll(pageRequest);
        return categoryPage;
    }

    @Override
    public void saveOrUpdate(Category category,String categoryid) {
        if (categoryDAO.existsById(categoryid)){
            Category oldCategory = categoryDAO.findById(categoryid).get();
            BeanUtils.copyProperties(category,oldCategory);
            categoryDAO.saveAndFlush(oldCategory);
        }else {
            categoryDAO.saveAndFlush(category);
        }

    }

    @Override
    public void deleteOne(String categoryId) {
        categoryDAO.deleteById(categoryId);
    }

    @Override
    public void deleteMore(List<String> categoryIdList) {
        for (String categoryId : categoryIdList){
            categoryDAO.deleteById(categoryId);
        }
    }
}
